<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Edit Contact</title>
<link rel="stylesheet" type="text/css" href="../style.css">
</head>
<body>
<h3>Select the Contact to Edit(Leave blank the Field not to be changed)</h3>
<form action="#" method="post">
Select Group:<br/>
<select name="id">
<option value="N/A">Select a Contact's Name from Dropdown</option>
<?php 
$conn=mysql_connect("localhost","root","") or die("Could not Establish Connection".mysql_error());
$db=mysql_select_db("contacts_ms",$conn) or die("No DB Selected".mysql_error());
$st="EM";
$qry=mysql_query("SELECT * FROM info_contacts");
while($r=mysql_fetch_array($qry)){
	/*
	*This code selects the contacts that are available in the system for editing.
	*/
	?>
		<option value="<?php echo $r['contact_id'] ?>"> <?php echo $r['fname'].' '.$r['sname'] ?></option>
		<?php
	
}
?>
</select><br/>
New First Name:<br/>
<input type="text" name="fname" size="30" class="txt"><br/>
New Second Name:<br/>
<input type="text" name="sname" size="30" class="txt"><br/>
New Email:<br/>
<input type="text" name="email" size="30" class="txt"><br/>
New Phone Number:<br/>
<input type="text" name="phone" size="30" class="txt"><br/><br/>
<input type="submit" value="Edit Contact" name="submit"  class="sbtns">&emsp;&emsp;&emsp;
<input type="reset" name="reset" value="Cancel" class="sbtns">
</form>
<?php
if(isset($_POST['submit'])){
	$id=$_POST['id'];
	$fname=$_POST['fname'];
	$sname=$_POST['sname'];
	$email=$_POST['email'];
	$phone=$_POST['phone'];
	/*
	*This code updates the Contacts details according to the inputs provided and skipping the fields that have not yet been 
	*provided!
	*It does this by checking the states of the input fields.
	*/
	if($fname && $sname && $email && $phone){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',sname='$sname',email='$email',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$fname){
		$th=mysql_query("UPDATE info_contacts SET sname='$sname',email='$email',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$sname){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',email='$email',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$email){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',sname='$sname',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$phone){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',sname='$sname',email='$email' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$sname){
		$th=mysql_query("UPDATE info_contacts SET email='$email',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$email){
		$th=mysql_query("UPDATE info_contacts SET sname='$sname',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$phone){
		$th=mysql_query("UPDATE info_contacts SET sname='$sname',email='$email' WHERE contact_id='$id'");
	}
	elseif(!$sname && !$email){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$sname && !$phone){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname',email='$email' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$sname && !$email){
		$th=mysql_query("UPDATE info_contacts SET phone='$phone' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$sname && !$phone){
		$th=mysql_query("UPDATE info_contacts SET email='$email' WHERE contact_id='$id'");
	}
	elseif(!$sname && !$email && !$phone){
		$th=mysql_query("UPDATE info_contacts SET fname='$fname' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$email && !$phone){
		$th=mysql_query("UPDATE info_contacts SET sname='$sname' WHERE contact_id='$id'");
	}
	elseif(!$fname && !$sname && !$email && !$phone) $th=false;
	if(!$th) echo "Could not Update the Contact Details.Please try again later!";
	else echo "Successfully Edited Contact's Details!";
}
?>
</body>
</html>